﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace BaiDuSitemap.Setting
{
    public partial class ucGroup : UserControl
    {
        #region Attributes

        private Thread myThread = null;
        private delegate void myDeleGateAddItem(DataRow dr);

        #endregion

        public ucGroup()
        {
            InitializeComponent();
        }

        private void ucGroup_Load(object sender, EventArgs e)
        {
            myThread = new Thread(new ThreadStart(ThreadLoadURL));
            myThread.Start();
        }

        private void ucGroup_SizeChanged(object sender, EventArgs e)
        {
            this.picClose.Location = new Point(this.Width - this.picClose.Width - 3, 3);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.statusStrip1.BackColor = Sitemap.AeroColor;
        }

        #region Event

        #region Button

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Group myGroup = new Group();
            try
            {
                myGroup.clv = this.lvGroup;
                myGroup.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myGroup.Dispose();
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.lvGroup.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择要修改的项目");
                return;
            }
            if (this.lvGroup.SelectedItems.Count > 1)
            {
                MessageBox.Show("每次只能修改一条记录");
                return;
            }

            Group myGroup = new Group();
            try
            {
                myGroup.isAdd = false;
                myGroup.clv = this.lvGroup;
                myGroup.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myGroup.Dispose();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.lvGroup.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择待删除项");
                return;
            }

            if (this.lvGroup.SelectedItems.Count == 1)
            {
                DeleteSingle(this.lvGroup.SelectedItems[0].SubItems[1].Text);
                return;
            }

            List<string> gnames = new List<string>();
            foreach (ListViewItem item in this.lvGroup.SelectedItems)
            {
                gnames.Add(item.SubItems[1].Text);
            }
            DeleteMulti(gnames);
        }

        #endregion

        #region Picture

        private void picClose_Click(object sender, EventArgs e)
        {
            if (myThread != null && myThread.IsAlive)
            {
                if (MessageBox.Show("正在加载数据，确认要关闭吗？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                {
                    return;
                }

                myThread.Abort();
            }

            this.Dispose();
        }

        private void picClose_MouseEnter(object sender, EventArgs e)
        {
            this.picClose.Image = Properties.Resources.close02;
        }

        private void picClose_MouseLeave(object sender, EventArgs e)
        {
            this.picClose.Image = Properties.Resources.close01;
        }

        #endregion

        #endregion

        #region Thread

        private void ThreadLoadURL()
        {
            DataTable dt = null;
            try
            {
                dt = DBO.Group.getAll();
                foreach (DataRow dr in dt.Rows) AddItem(dr);
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (dt != null) dt.Dispose();
            }
        }

        #endregion

        #region Method

        private void AddItem(DataRow dr)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new myDeleGateAddItem(AddItem), new object[] { dr });
            }
            else
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = (this.lvGroup.Items.Count + 1).ToString();
                item.SubItems.Add(dr["gname"].ToString());
                item.SubItems.Add(dr["comment"].ToString());
                this.lvGroup.Items.Add(item);
            }
        }

        public bool Closing()
        {
            if (myThread != null && myThread.IsAlive) return false;
            return true;
        }

        private void DeleteSingle(string gname)
        {
            int result = DBO.Group.Delete(gname);

            switch(result)
            {
                case -2:
                    MessageBox.Show("分组 "+gname+" 不存在");
                    break;
                case 1:
                    MessageBox.Show("删除成功");
                    this.lvGroup.Items.Remove(this.lvGroup.SelectedItems[0]);
                    break;
                default:
                    MessageBox.Show("操作失败，错误代码：" + result.ToString());
                    return;
            }
        }

        private void DeleteMulti(List<string> gnames)
        {
            int result = DBO.Group.Delete(gnames);

            if (result > 0)
            {
                MessageBox.Show(string.Format("删除成功,共删除{0}条记录", result.ToString()));
                foreach (ListViewItem item in this.lvGroup.SelectedItems) this.lvGroup.Items.Remove(item);
            }
            else MessageBox.Show("操作失败");
        }

        #endregion
    }
}
